package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.FollowCommon;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelatinDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApFollowMapper;
import com.heima.user.service.ApFollowService;
import com.heima.user.service.ApUserService;
import com.heima.utils.thread.ApThreadLocalUtils;
import org.jcp.xml.dsig.internal.dom.ApacheTransform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Service
public class ApFollowServiceImpl extends ServiceImpl<ApFollowMapper, ApUserFollow> implements ApFollowService {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ApUserService userService;

    @Override
    public ResponseResult followhandle(@RequestBody UserRelatinDto userRelatinDto) {

        //关注
        if (userRelatinDto.getOperation() == 0) {
            LambdaQueryWrapper<ApUserFollow> lambdaQueryWrapper = new LambdaQueryWrapper<>();

            lambdaQueryWrapper.eq(ApUserFollow::getUserid,userRelatinDto.getUserid());
            lambdaQueryWrapper.eq(ApUserFollow::getFollowid, userRelatinDto.getAuthorId());
            ApUserFollow follow = getOne(lambdaQueryWrapper);
            if (follow != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            ApUser user = userService.getById(userRelatinDto.getUserid());

            follow = new ApUserFollow();
            follow.setFollowid(userRelatinDto.getAuthorId());
            follow.setUserid(user.getId());
            follow.setFollowname(user.getName());
            follow.setCreatetime(new Date());
            save(follow);
            savetemplate(follow);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        LambdaQueryWrapper<ApUserFollow> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ApUserFollow::getUserid, userRelatinDto.getUserid());
        lambdaQueryWrapper.eq(ApUserFollow::getFollowid, userRelatinDto.getAuthorId());
        ApUserFollow follow = getOne(lambdaQueryWrapper);
        if (follow == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<ApUserFollow> lambdaQueryWrapper1 = new LambdaQueryWrapper();
        lambdaQueryWrapper1.eq(ApUserFollow::getUserid, userRelatinDto.getUserid());
        lambdaQueryWrapper1.eq(ApUserFollow::getFollowid, userRelatinDto.getAuthorId());
        remove(lambdaQueryWrapper1);
        deltemplate(follow);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private void deltemplate(ApUserFollow follow) {
        String redisKey= FollowCommon.follow+follow.getFollowid();
        String hashKey=FollowCommon.follow+follow.getUserid();
        redisTemplate.opsForHash().delete(redisKey,hashKey);
    }

    /**
     * 从redis中查询是否点赞
     * @param follow
     */
    private void savetemplate(ApUserFollow follow) {
        String redisKey= FollowCommon.follow+follow.getFollowid();
      String hashKey=FollowCommon.follow+follow.getUserid();

        Object data = redisTemplate.opsForHash().get(redisKey, hashKey);
        if (data==null){
            redisTemplate.opsForHash().put(redisKey,hashKey,0);
        }

    }

}
